package com.chaopei.base2;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author Corday 天涯
 * @Date 2022/3/6 23:17
 */
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {//重写后,内容相同的对象不重复了
        return this.name.hashCode()+this.age<<2;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Person){
         Person p =(Person)obj;
         return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "姓名:"+this.name+" 年龄:"+this.age;
    }
}
class  MyMainp{
    public static void main(String[] args) {
        Person p1 = new Person("tom",19);
        Person p2 = new Person("tom",19);
        //Object o = new Person();
        //System.out.println(p.equals(o));
        Set<Person> set = new HashSet<>();
        set.add(p1);
        set.add(p2);
        for(Person t:set){
            System.out.println(t);
        }
    }
}